package com.jh3502.helper.function.person.personlist;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jh3502.helper.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PersonListAdapter extends RecyclerView.Adapter<PersonListAdapter.ViewHolder> implements View.OnClickListener, View.OnLongClickListener {
    Context mContext;


    private List<PersonListBean> dataList;
    private View convertView;
    @Nullable
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    @Nullable
    private OnRecyclerViewItemLongClickListener mOnItemLongClickListener = null;

    public PersonListAdapter(Context context, List<PersonListBean> dataList) {
        this.dataList = dataList;
        this.mContext = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        convertView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_person, viewGroup, false);
        ViewHolder vh = new ViewHolder(convertView);
        //将创建的View注册点击事件
        convertView.setOnClickListener(this);
        convertView.setOnLongClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int position) {
        viewHolder.itemView.setTag(position + "");
        if(position%2!=0){
            viewHolder.itemPerson.setBackgroundColor(ContextCompat.getColor(mContext,R.color.gray_bg));
        }else{
            viewHolder.itemPerson.setBackgroundColor(Color.WHITE);
        }

        PersonListBean personMap = dataList.get(position);
        if (personMap == null) {
            return;
        }
        viewHolder.itemPersonNum.setText(position + "");
        viewHolder.itemPersonDep.setText(personMap.getDep());
        viewHolder.itemPersonName.setText(personMap.getName());
        if (personMap.getSex() == 0) {
            viewHolder.itemPersonSex.setText("男");
        } else {
            viewHolder.itemPersonSex.setText("女");
        }
    }

    @Override
    public void onClick(@NonNull View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick((String) v.getTag());
        }
    }

    @Override
    public boolean onLongClick(@NonNull View v) {
        if (mOnItemLongClickListener != null) {
            mOnItemLongClickListener.onItemLongClick((String) v.getTag());
        }
        return true;
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public void setOnItemLongClickListener(OnRecyclerViewItemLongClickListener listener) {
        this.mOnItemLongClickListener = listener;
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(String s);
    }

    public interface OnRecyclerViewItemLongClickListener {
        void onItemLongClick(String s);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_person_num)
        TextView itemPersonNum;
        @BindView(R.id.item_person_dep)
        TextView itemPersonDep;
        @BindView(R.id.item_person_name)
        TextView itemPersonName;
        @BindView(R.id.item_person_sex)
        TextView itemPersonSex;
        @BindView(R.id.item_person)
        LinearLayout itemPerson;
        public ViewHolder(@NonNull View view) {
            super(view);
            ButterKnife.bind(this, view);

        }
    }

}